package BehaviorModel.iterorModel;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class InfoReserveCollege implements College{

    private List<Department> departments;

    private int sizeOfDepartment = 0; // 集合大小

    private Iterator iterator;

    public InfoReserveCollege(List<Department> departments) {
        this.departments = departments;
        this.sizeOfDepartment = departments.size();
    }

    public void setDepartments(List<Department> departments) {
        this.departments = departments;
        this.sizeOfDepartment = departments.size();
    }

    @Override
    public String getName() {
        return "物理系逆序";
    }

    @Override
    public void addDepartment(Department department) {
        departments.add(department);
        sizeOfDepartment++;
        createIterator();
    }

    @Override
    public void removeDepartment(Department department) {
        ArrayList<Department> departmentArrayList = new ArrayList<>();

        for (int i = 0; i < sizeOfDepartment; i++) {
            if (departments.get(i).equals(department)) {
                departmentArrayList.add(departments.get(i));
            }
        }
        this.sizeOfDepartment--;
        this.departments.removeAll(departmentArrayList);
    }

    @Override
    public Iterator createIterator() {
        if (null == iterator){
            return new InfoReserveIteror(departments);
        }else {
            InfoReserveIteror infoReserveIteror = (InfoReserveIteror) this.iterator;
            infoReserveIteror.setDepartments(departments);
        }
        return this.iterator;
    }
}
